ENT.Type = "anim"
ENT.Base = "garc_base"

ENT.PrintName		= ""
ENT.Author			= ""
ENT.Contact			= ""
ENT.Purpose			= ""
ENT.Instructions	= ""

ENT.Model = "models/Combine_turrets/Floor_turret.mdl"	
ENT.DEBUG = false

ENT.MaxYaw = 60
ENT.MaxPitch = 30 //Actually 15, but that is too small
ENT.MaxDistance = 1000

function ENT:ExtraNW()
	return {
		{"Enemy", NULL, NWTYPE_ENTITY },
	}
end

function ENT:InViewCone( ent )
	local ang = self:GetTargetAngle( ent )
	
	return math.abs(ang.y) < self.MaxYaw && math.abs(ang.p) < self.MaxPitch
end

function ENT:CanShoot( ent )

	if !self:InViewCone( ent ) then
		return false
	end
	
	local tracetbl = {
		start = self:ShootPos(),
		endpos = ent:GetShootPos(),
		filter = self
	}
	
	if tracetbl.start:Distance( tracetbl.endpos ) > self.MaxDistance then
		return false
	end
	
	local trace = util.TraceLine( tracetbl )
	
	if self.DEBUG then
		Msg("Trace hit entity: ", trace.Entity, " - ", trace.Fraction , "\n")
	end
	
	return trace.Fraction >= 0.99 || trace.Entity == ent
end

function ENT:GetTargetAngle( ent )
	local MyPos = self:WorldToLocal( self:ShootPos() )
	local pos = self:WorldToLocal( ent:GetShootPos() )
	local ang = ( pos - MyPos ):Normalize():Angle()
	
	if ang.p > 180 then
		ang.p = ang.p - 360
	end
	
	if ang.y > 180 then
		ang.y = ang.y - 360
	end

	return ang
end

function ENT:UpdateAnglesEnt( ent )

	local ang = self:GetTargetAngle( ent )
	
	self:UpdateAngles( ang.p, ang.y )

end

function ENT:UpdateAngles( pitch, yaw )

	self:SetPoseParameter("aim_pitch", math.Clamp( pitch, -15, 15 ) )
	self:SetPoseParameter("aim_yaw",  math.Clamp( yaw, -60, 60 ) )
	
end

function ENT:ShootPos()
	return self:GetAttachment( self.LightAttachment ).Pos
end